package com.veng.springbootdemo.controller;

import com.veng.springbootdemo.service.FileUploadService;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;

@Controller
public class UploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @GetMapping(value = "upload")
    public String upload(){
        return "uploadFile";
    }

    @RequiresGuest
    @PostMapping(value = "upload")
    @ResponseBody
    public String uploadFile(@RequestParam("file")MultipartFile file) throws IOException {
        String upload = fileUploadService.upload(file);
        if (upload.equals("true")) {
            return "上传成功";
        }
        return "上传失败";
    }
}
